package cn.t17.appsys.controller;

import cn.t17.appsys.dto.CommonResponse;
import cn.t17.appsys.entity.AppInfo;
import cn.t17.appsys.service.AppInfoService;
import cn.t17.appsys.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * AppInfoController class
 *
 * @author Administrator
 * @date 2021/9/16
 */
@CrossOrigin(allowCredentials = "true")
@RestController
@RequestMapping("app")
public class AppInfoController extends BaseController {

    @Autowired
    private AppInfoService appInfoService;

    // @RequestMapping(value = "query",method = RequestMethod.GET)
    // public CommonResponse<List<AppInfo>> query(AppInfo info){
    //     List<AppInfo> appInfos = appInfoService.listBy(info);
    //     return success(appInfos);
    // }

    @RequestMapping(value = "query",method = RequestMethod.GET)
    public CommonResponse<PageVO> query(AppInfo info,
                                        @RequestParam(defaultValue = "1") Integer pageIndex,
                                        @RequestParam(defaultValue = "2") Integer pageSize){
        // List<AppInfo> appInfos = appInfoService.listBy(info);
        List<AppInfo> appInfos = appInfoService.listByPage(info,pageIndex,pageSize);
        Integer total= appInfoService.countBy(info);

        PageVO pageVO = new PageVO();
        pageVO.setTotal(total);
        pageVO.setList(appInfos);

        return success(pageVO);
    }

    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public CommonResponse<?> deleteById(@PathVariable Integer id){
        appInfoService.deleteById(id);
        return success("删除成功");
    }

    @RequestMapping(value = "update",method = RequestMethod.PUT)
    // 如果请求是以json格式发送数据的话，接收对象参数前要加 @RequestBody
    public CommonResponse<?> update(@RequestBody  AppInfo info){
        appInfoService.update(info);
        return success("修改成功");
    }
}
